<?php
// +----------------------------------------------------------------------
// | 有理想的地方，地狱都是天堂。
// +----------------------------------------------------------------------
// | Copyright @ df81.com 版权所有
// +----------------------------------------------------------------------
// | 作者: 南国老符 邮箱：1062129401@qq.com
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\admin\model\AuthRule as authRuleModel;
use think\facade\Db;
use think\facade\View;

class AuthRule extends Base{
    // 列表
    public function index(){
        $keyword = input('keyword'); $order = ['sequence'=>'desc','id'=>'desc'];
		$rs = authRuleModel::index($this->lang,$keyword,$order);

		View::assign('list',$rs);
		View::assign('total',count($rs));
		
        return view();
    }

    // 新增编辑
    public function edit($id=0){
        if(!request()->isPost()){
            $rs = authRuleModel::find($id);
            View::assign('rs',$rs);
            View::assign("authRule",getChildSort(Db::name('auth_rule')->where('modulesign','web')->select()));
            
            return view();
        }else{
            try {
                $rs = authRuleModel::edit($id);
                return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('id'=>$id)]);
            } catch (Exception $e) {
                return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
            }
        }		
	}
	
	// 单条删除
    public function delete_one($id){
        $id = input('id');
        //禁止删除含有子项的父项
        $hasChild = Db::name('auth_rule')->where('parentid',$id)->find();
        if(!empty($hasChild)){
            return json(['code' => 1001, 'msg' => lang('c_delete_sort_child_first'), 'data' => array('id'=>$id)]);
        }
        
        try {
            //执行删除
            Db::name('auth_rule')->delete($id);
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('id'=>$id)]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
   }	
	
	// 批量删除
    public function delete_all(){
		$selectid = input('selectid/a');
		if(empty($selectid)){
            return json(['code' => 1001, 'msg' => lang('c_delete_check'), 'data' => array('selectid'=>$selectid)]);
        }
		
		foreach($selectid as $i => $rs){
		    $id = $selectid[$i];
            //禁止删除含有子项的父项
            $hasChild = Db::name('auth_rule')->where('parentid',$id)->find();
            if(!empty($hasChild)){
                return json(['code' => 1002, 'msg' => lang('c_delete_sort_child_first'), 'data' => array('id'=>$id)]);
                break;
            }
            $deleteid[] = $id;
	    }
        
        try {
            Db::name('auth_rule')->delete($deleteid);
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('deleteid'=>$deleteid)]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }	
}